package de.uni.muenster.idw2.location.wifi;

import java.util.ArrayList;

import android.util.Log;

public class ResultAverage {
	ArrayList<Result> firstsignal;
	ArrayList<Result> secondsignal;
	ArrayList<Result> thirdsignal;
	String bssid;
	int level1=0;
	int level2=0;
	int level3=0;
	int level4=0;
	int level5=0;
	int level6=0;
	String s1 ="02:20:a6:28:31:20";
	String s2 ="02:20:a6:60:8c:a0";
	String s3 ="02:20:a6:64:00:20";
	String s4 ="02:20:a6:64:03:60";
	String s5 ="02:20:a6:66:5e:40";
	String s6 ="02:20:a6:66:6a:30";
	boolean reachedfs1 = false;
	boolean reachedfs2 = false;
	boolean reachedfs3 = false;
	boolean reachedfs4 = false;
	boolean reachedfs5 = false;
	boolean reachedfs6 = false;
	boolean reachedss1 = false;
	boolean reachedss2 = false;
	boolean reachedss3 = false;
	boolean reachedss4 = false;
	boolean reachedss5 = false;
	boolean reachedss6 = false;
	boolean reachedts1 = false;
	boolean reachedts2 = false;
	boolean reachedts3 = false;
	boolean reachedts4 = false;
	boolean reachedts5 = false;
	boolean reachedts6 = false;
	
	
	
	
	public ResultAverage(ArrayList<Result> fs, ArrayList<Result> ss ,ArrayList<Result> ts){
		firstsignal = fs;
		secondsignal = ss;
		thirdsignal = ts;
	}
	
	
	public ArrayList<Result> average(){
		
		
		for(Result r: firstsignal){
			bssid = r.getBssid();
			if (s1.equals(r.getSsid())) {level1 = r.getLevel(); reachedfs1 = true;}
			if (s2.equals(r.getSsid())) {level2 = r.getLevel(); reachedfs2 = true;}
			if (s3.equals(r.getSsid())) {level3 = r.getLevel(); reachedfs3 = true;}
			if (s4.equals(r.getSsid())) {level4 = r.getLevel(); reachedfs4 = true;}
			if (s5.equals(r.getSsid())) {level5 = r.getLevel(); reachedfs5 = true;}
			if (s6.equals(r.getSsid())) {level6 = r.getLevel(); reachedfs6 = true;}
			
		}
		
		if(reachedfs1 == false){level1 = -100;}
		if(reachedfs2 == false){level2 = -100;}
		if(reachedfs3 == false){level3 = -100;}
		if(reachedfs4 == false){level4 = -100;}
		if(reachedfs5 == false){level5 = -100;}
		if(reachedfs6 == false){level6 = -100;}
		
		for(Result s: secondsignal){
			if (s1.equals(s.getSsid())) {level1 = level1 + s.getLevel(); reachedss1 = true;}
			if (s2.equals(s.getSsid())) {level2 = level2 + s.getLevel(); reachedss2 = true;}
			if (s3.equals(s.getSsid())) {level3 = level3 + s.getLevel(); reachedss3 = true;}
			if (s4.equals(s.getSsid())) {level4 = level4 + s.getLevel(); reachedss4 = true;}
			if (s5.equals(s.getSsid())) {level5 = level5 + s.getLevel(); reachedss5 = true;}
			if (s6.equals(s.getSsid())) {level6 = level6 + s.getLevel(); reachedss6 = true;}
		}
		
		if(reachedss1 == false){level1 = level1 + (-100);}
		if(reachedss2 == false){level2 = level2 + (-100);}
		if(reachedss3 == false){level3 = level3 + (-100);}
		if(reachedss4 == false){level4 = level4 + (-100);}
		if(reachedss5 == false){level5 = level5 + (-100);}
		if(reachedss6 == false){level6 = level6 + (-100);}
		
		for(Result t: thirdsignal){
			if (s1.equals(t.getSsid())) {level1 = level1 + t.getLevel(); reachedts1 = true;}
			if (s2.equals(t.getSsid())) {level2 = level2 + t.getLevel(); reachedts2 = true;}
			if (s3.equals(t.getSsid())) {level3 = level3 + t.getLevel(); reachedts3 = true;}
			if (s4.equals(t.getSsid())) {level4 = level4 + t.getLevel(); reachedts4 = true;}
			if (s5.equals(t.getSsid())) {level5 = level5 + t.getLevel(); reachedts5 = true;}
			if (s6.equals(t.getSsid())) {level6 = level6 + t.getLevel(); reachedts6 = true;}
		}
		
		if(reachedts1 == false){level1 = level1 - 100;}
		if(reachedts2 == false){level2 = level2 - 100;}
		if(reachedts3 == false){level3 = level3 - 100;}
		if(reachedts4 == false){level4 = level4 - 100;}
		if(reachedts5 == false){level5 = level5 - 100;}
		if(reachedts6 == false){level6 = level6 - 100;}
		
		level1 = (int) level1/3;
		level2 = (int) level2/3;
		level3 = (int) level3/3;
		level4 = (int) level4/3;
		level5 = (int) level5/3;
		level6 = (int) level6/3;
		
		ArrayList<Result> averageRes = new ArrayList<Result>();
		
		averageRes.add(new Result(bssid, s1,level1));
		averageRes.add(new Result(bssid, s2,level2));
		averageRes.add(new Result(bssid, s3,level3));
		averageRes.add(new Result(bssid, s4,level4));
		averageRes.add(new Result(bssid, s5,level5));
		averageRes.add(new Result(bssid, s6,level6));
		
		//Log.v("show", ""+s1+ " "+level1+"");
		//Log.v("show", ""+s2+ " "+level2+"");
		//Log.v("show", ""+s3+ " "+level3+"");
		//Log.v("show", ""+s4+ " "+level4+"");
		//Log.v("show", ""+s5+ " "+level5+"");
		//Log.v("show", ""+s6+ " "+level6+"");
		
		return averageRes;
	}
	
}
